/**
 * Created by sxx on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";


let token = sessionStorage.getItem('supplier_token');
let projId = getArgs().id;
if(token==''||token==null){
    location.href = HOST_FRT+'/supplier_self/login.html';
}
if(projId==''||projId==null){
    location.href = HOST_FRT+'/supplier_self/my_bidding_success.html';
}
var MyBidResultView = {
    myWinDataList : [],
    init: function(){
        this.getBasicInfo()
            .then(this.getResultDetail)
            .then(this.renderBidList);
    },
    // 获取项目基本信息
    getBasicInfo:function(){
        return new Promise((resolve, reject) => {
            $.ajax({
                url: HOST_ADDR+'/cpmp/bidProjOnMngForSplr/viewProjDtl/'+projId,
                type: 'get',
                dataType: 'json',
                contentType:"application/json",
                headers: {
                    'access_token': token,
                    'login_type': 'splr'
                },
                success: function(resp) {
                    if (resp.success) {
                        resolve(resp);
                    }else{
                        alert(resp.msgInf);
                        console.log(resp.msgCod + ' ' + resp.msgInf);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        });
    },

    getResultDetail:function(data){
        return new Promise((resolve, reject) => {
            $.ajax({
                url: HOST_ADDR+'/cpmp/bidProjOnMngForSplr/viewProjRstDtl/'+projId,
                type: 'get',
                dataType: 'json',
                contentType:"application/json",
                headers: {
                    'access_token': token,
                    'login_type': 'splr'
                },
                success: function(resp) {
                    if (resp.success) {
                        // self.myWinDataList = resp.data;
                        console.log(resp);
                        data.data.myWinDataList = resp.data;
                        resolve(data);
                    }else{
                        alert(resp.msgInf);
                        console.log(resp.msgCod + ' ' + resp.msgInf);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        });
    },

    renderBidList:function(resp){
        return new Promise((resolve, reject) => {
            let projqotCntTyp='非实时报价';
            let bidRngTyp='非定向招标';
            let bidOpenTim = '';
            let bidEndTim = '';
            let projMemo = '';
            let splrNames = '';
            if (resp.data.bidProjOn.qotCntTyp == 2) {
                projqotCntTyp='实时报价';
            }
            if (resp.data.bidProjOn.bidRngTyp == 1) {
                bidRngTyp='定向招标';
            }
            if (resp.data.bidProjOn.bidOpenTim != null&&resp.data.bidProjOn.bidOpenTim !='') {
                bidOpenTim = (new Date(resp.data.bidProjOn.bidOpenTim)).Format("yyyy-MM-dd hh:mm:ss");
            }
            if (resp.data.bidProjOn.bidEndTim != 'null'&&resp.data.bidProjOn.bidEndTim !='') {
                bidEndTim = (new Date(resp.data.bidProjOn.bidEndTim)).Format("yyyy-MM-dd hh:mm:ss");
            }
            if (resp.data.bidProjOn.projMemo != null&&resp.data.bidProjOn.projMemo !='') {
                projMemo = resp.data.bidProjOn.projMemo;
            }
            if(resp.data.splrs){
                $.each(resp.data.splrs,function(index,item){
                    splrNames += item.splrNam + " ";
                });
            }
            var matTyp = resp.data.bidProjOn.matTyp;
            if(resp.data.matTypDesc!=null && resp.data.matTypDesc!=''){
                matTyp = resp.data.matTypDesc;
            }
            jQuery('.basicInfo').append('<div class="portlet-body"><table class="table table-striped table-bordered"><tbody><tr>' +
                '<td width="16%">项目名称：</td><td colspan="2">'+resp.data.bidProjOn.projNam+'</td> ' +
                '<td width="16%">项目编号：</td> <td colspan="2">'+resp.data.bidProjOn.projNbr+'</td></tr> <tr><tr>' +
                '<td width="16%">物料品类：</td><td colspan="2">'+matTyp+'</td>' +
                '<td width="16%">报价次数类型：</td><td colspan="2">'+projqotCntTyp+'</td></tr> <tr>' +
                '<td width="16%">招标单位：</td><td colspan="2">'+resp.data.bidProjOn.bidDptNam+'</td>' +
                '<td width="16%">招标单位地址：</td><td colspan="2">'+resp.data.bidProjOn.bidDptAddr+'</td></tr><tr>' +
                '<td width="16%">投标保证金（元）：</td><td width="16%">'+resp.data.bidProjOn.bidDpst+'</td>' +
                '<td width="16%">联系人：</td><td width="16%">'+resp.data.bidProjOn.ctct+'</td>' +
                '<td width="16%">联系电话：</td><td width="16%">'+resp.data.bidProjOn.ctctTel+'</td></tr><tr>' +
                '<td width="16%">投标开标时间：</td><td colspan="2">'+bidOpenTim+'</td>' +
                '<td width="16%">投标截止时间：</td><td colspan="2">'+bidEndTim+'</td></tr><tr>' +
                '<td>招标范围类型：</td><td colspan="2">'+bidRngTyp+'</td>' +
                '<td width="16%">指定招标供应商：</td><td colspan="2">'+splrNames+'</td></tr></tr><tr>' +
                '<td>备注信息：</td><td colspan="5">'+projMemo+'</td></tr></tbody></table></div>');
            if(resp.data.matDtls.length ==0){
                jQuery('#emptyData').hide();
            }else{
                jQuery('#emptyData').show();
            }
            for(var i = 0;i< resp.data.matDtls.length;i++) {
                if(resp.data.matDtls[i].memo == null){
                    resp.data.matDtls[i].memo = "";
                }
                if(resp.data.matDtls[i].techServ == null){
                    resp.data.matDtls[i].techServ = "";
                }
                let myWinNum = 0;
                let myWinPri = 0;
                let myWinSumPri = 0;
                for(var j=0;j<resp.data.myWinDataList.length;j++){
                    if(resp.data.matDtls[i].matCod==resp.data.myWinDataList[j].matCod){
                        myWinNum = resp.data.myWinDataList[j].pchsNum;
                        myWinPri = resp.data.myWinDataList[j].untPri;
                        myWinSumPri = resp.data.myWinDataList[j].ttlPri;
                    }
                }
                jQuery('#new_basicInfo').after('<tr>' +
                    '<td>'+resp.data.matDtls[i].matNam+'</td>'+
                    '<td>'+resp.data.matDtls[i].pchsNum+'</td>'+
                    '<td>'+resp.data.matDtls[i].matUnt+'</td>'+
                    '<td>'+myWinNum+'</td>'+
                    '<td>'+myWinPri+'</td>'+
                    '<td>'+myWinSumPri+'</td>'+
                    '<td>'+resp.data.matDtls[i].techServ+'</td>'+
                    '<td>'+resp.data.matDtls[i].memo+'</td></tr>'
                );
            }

            resolve();
        });
    }
};

App.init();
MyBidResultView.init();

$('.J_return').click(function(){
    window.history.back();
});
